package com.matrixxun.zoominlist.slice;

import com.matrixxun.starry.PullToZoomListView;
import com.matrixxun.zoominlist.ResourceTable;
import com.matrixxun.zoominlist.adapter.ListItemProvider;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.ListContainer;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.window.dialog.ToastDialog;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

/**
 * MainAbility
 */
public class MainAbilitySlice extends AbilitySlice {
    private static final HiLogLabel LOG_LABEL = new HiLogLabel(HiLog.LOG_APP, 0x20221, "MainAbilitySlice");
    private String[] adapterData =
            new String[] {
                "Activity",
                "Service",
                "Content Provider",
                "Intent",
                "BroadcastReceiver",
                "ADT",
                "Sqlite3",
                "HttpClient",
                "DDMS",
                "Studio",
                "Fragment",
                "Loader",
                "ADT",
                "Sqlite3",
                "HttpClient",
                "DDMS",
                "Studio",
                "Fragment",
                "Loader"
            };
    ;
    private ToastDialog mToastDialog;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        PullToZoomListView pullToZoomListView = (PullToZoomListView) findComponentById(ResourceTable.Id_list_view);
        ListItemProvider itemProvider = new ListItemProvider(this, adapterData, ResourceTable.Layout_list_item);
        pullToZoomListView.setItemProvider(itemProvider);
        ShapeElement shapeElement = new ShapeElement(this, ResourceTable.Graphic_list_divider);
        pullToZoomListView.setBoundary(shapeElement);
        pullToZoomListView.setBoundarySwitch(true);
        pullToZoomListView.setReboundEffect(false);
        pullToZoomListView.setHeaderBoundarySwitch(false);
        pullToZoomListView.getHeaderView().setPixelMap(ResourceTable.Media_img_header);
        pullToZoomListView.getHeaderView().setScaleMode(Image.ScaleMode.CLIP_CENTER);
        pullToZoomListView.setHeaderProvider(this, itemProvider);
        pullToZoomListView.setItemClickedListener(
                new ListContainer.ItemClickedListener() {
                    @Override
                    public void onItemClicked(ListContainer listContainer, Component component, int position, long id) {
                        HiLog.info(LOG_LABEL, "onItemClicked : " + position );
                        if (mToastDialog != null && mToastDialog.isShowing()) {
                            mToastDialog.remove();
                        } else {
                            mToastDialog = new ToastDialog(getContext());
                        }
                        mToastDialog.setText("onItemClicked : " + position);
                        mToastDialog.show();
                    }
                });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
